package com.wnhz.work.yz.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.AllocationAdapter;
import com.wnhz.work.yz.adapter.SingleAllocationAdapter;
import com.wnhz.work.yz.bean.AllocationBean;
import com.wnhz.work.yz.bean.SingleAllocationBean;
import com.wnhz.work.yz.bean.SingleItemBean;
import com.wnhz.work.yz.constant.Constants;
import com.wnhz.work.yz.molder.EntiretyGoodsMolder2;
import com.wnhz.work.yz.molder.EntiretyMolder1;
import com.wnhz.work.yz.molder.EntiretyShoppingMolder;
import com.wnhz.work.yz.utils.BroadCastReceiverUtil;
import com.wnhz.work.yz.utils.CalculateUtil;
import com.wnhz.work.yz.utils.LogUtil;
import com.wnhz.work.yz.utils.MyUtils;
import com.wnhz.work.yz.utils.Prefer;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.lang.ref.WeakReference;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * ---单套配置
 */
public class SingleAllocationActivity extends BaseActivity implements View.OnClickListener, BroadCastReceiverUtil.OnReceiveBroadcast {
    private static final String TAG = "SingleAllocationActivit";
    private static final int REPLACE = 1001;//替换
    private static final int ADDEDWASH = 1002;//新增
    private Context context = this;
    private TextView title;
    private TextView tv_right;
    private ListView my_listView;
    private List<SingleAllocationBean> beanList = new ArrayList<>();
    private SingleAllocationAdapter mAdapter;
    private List<SingleItemBean> cateList = new ArrayList<>();

    private SingleAllocationBean singleAllocationBean = new SingleAllocationBean();
    private BroadcastReceiver broadcastReceiver;

    private String packageId;
    private String skuId;
    private String colorId;
    private TextView tv_all_price;
    private Double replaceprice = 0.00;
    private String currentPrice;
    private String flag;
    private List<String> list = new ArrayList<>();
    private List<String> valueList = new ArrayList<>();

    double chanjiaPrice;
    private List<AllocationBean> list1;
    private EntiretyMolder1 entiretyMolder1;
    private double all_endPrice = 0.00;
    private double changePrice = 0.00;
    private double ha;
    private Double ha1;
    private Double sysnum = 0.00;
    private double prmaxposition = 0.00;
    private double prposition = 0.00;
    private String goods_name;//商品的名字
    private int index2 = -1;
    public static final int test = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_allocation);

        initData();
        initView();
        upload();

        broadcastReceiver = BroadCastReceiverUtil.registerBroadcastReceiver(SingleAllocationActivity.this, new String[]{Constants.UPDATE_FIRST_PRICE, Constants.UPDATE_FIRST_PRICE, Constants.UPDATE_PRICE_ADD, Constants.UPDATE_PRICE_DEL}, this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BroadCastReceiverUtil.unRegisterBroadcastReceiver(SingleAllocationActivity.this, broadcastReceiver);
    }


    private void initData() {
        packageId = getIntent().getStringExtra("packageId");
        skuId = getIntent().getStringExtra("skuId");
        colorId = getIntent().getStringExtra("colorId");
        if (!TextUtils.isEmpty(getIntent().getStringExtra("goods_name"))){
            goods_name = getIntent().getStringExtra("goods_name");
        }
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("单套配置");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        tv_right = (TextView) findViewById(R.id.submit);
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setText("下一步");
        tv_right.setTextColor(0xff008c99);
        tv_all_price = (TextView) findViewById(R.id.tv_all_price);
        findViewById(R.id.submit).setVisibility(View.VISIBLE);
        findViewById(R.id.tv_reset).setOnClickListener(this);
        my_listView = (ListView) findViewById(R.id.my_listView);
    }

    private void upload() {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("token", MyApplication.getInstance().getUserBean().getToken());
        } catch (Exception e) {
            return;
        }
        map.put("packageId", packageId);
//        map.put("skuId", skuId);
        XUtil.Post(Url.PACKAGE_PACKAGELINKPRICING, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess:colorId= " + result.substring(0, result.length() / 3));
                Log.e(TAG, "onSuccess:colorId= " + result.substring(result.length() / 3, (result.length() / 3 * 2)));
                Log.e(TAG, "onSuccess:colorId= " + result.substring((result.length() / 3 * 2), result.length()));

                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    Log.e("列表信息===", result);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        SingleAllocationBean allocationBean = gson.fromJson(jsonObject.optString("info"), SingleAllocationBean.class);
                        singleAllocationBean = gson.fromJson(jsonObject.optString("info"), SingleAllocationBean.class);
                        beanList.add(singleAllocationBean);
                        double opice = Double.parseDouble(singleAllocationBean.getOprice());
                        double total = Double.parseDouble(singleAllocationBean.getTotal_price());
                        DecimalFormat df = new DecimalFormat("######0.00");
                        double sys = opice / total;
                        sysnum = Double.valueOf(df.format(sys));

                        Log.e("===浴室系数==", sysnum + "");
                        Log.e("priceflag", "" + MyApplication.getInstance().getPricefale());
                        if (MyApplication.getInstance().getPricefale() == 1) {
                            MyApplication.getInstance().setSingleAllocationBean(allocationBean);
                            MyApplication.getInstance().setPricefale(2);
                        }
                        for (int i = 0; i < beanList.size(); i++) {
                            //父级列表
                            cateList = beanList.get(i).getCate_info();
                            //can_buy == 1 显示出厂价，can_buy == 0 显示市场指导价
                            //oprice是出厂价（核价下单用），price市场指导价（直接购买时用）
                            if (beanList.get(i).getCan_buy().equals("1")) {
                                tv_all_price.setText(getPriceDoule(beanList.get(i).getOprice()));
                                Prefer.getInstance().setMarketPrice(beanList.get(i).getOprice());
                            } else {
                                tv_all_price.setText(getPriceDoule(beanList.get(i).getPrice()));
                                Prefer.getInstance().setMarketPrice(beanList.get(i).getOprice());
                            }
                            MyApplication.getInstance().setPackagePrice(beanList.get(i).getPrice());
                            Log.e("套餐价格", beanList.get(i).getPrice());
                        }

                        chanjiaPrice = Double.parseDouble(singleAllocationBean.getPrice()) - Double.parseDouble(getPrice());

                        mAdapter = new SingleAllocationAdapter(SingleAllocationActivity.this, cateList, SingleAllocationActivity.this, my_listView);

                        mAdapter.setMyMenuItemClickListener(new SingleAllocationAdapter.MyMenuItemClickListener() {
                            @Override
                            public void onMenuClick(SwipeMenuListView sm_list, AllocationAdapter mAdapter, int max_position, int position, SwipeMenu menu, int index) {
                                switch (index) {
                                    case 0:
                                        if (MyUtils.isFastClick()) {
                                            //点击替换跳转到替换商品界面（传值过去）
                                            Intent intent = new Intent(context, ReplaceActivity.class);
                                            intent.putExtra("goodsId", singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_id());
                                           Log.v(TAG, "old=="+singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_id());
                                            intent.putExtra("max_position", max_position);
                                            intent.putExtra("position", position);
                                            intent.putExtra("packageId", packageId);


                                            intent.putExtra("nums", singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_nums());
                                            intent.putExtra("is_old", singleAllocationBean.getCate_info().get(max_position).getData().get(position).getIs_old());

                                            MyApplication.getInstance().setYsnum(Double.parseDouble(singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_nums()));
                                            startActivityForResult(intent, REPLACE);
                                            mAdapter.notifyDataSetChanged();
                                            sm_list.smoothOpenMenu(position);
                                            SingleAllocationActivity.this.mAdapter.notifyDataSetChanged();
                                            replaceprice = Double.parseDouble(singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_price());
                                            MyApplication.getInstance().setPackagePrice(tv_all_price.getText().toString());
                                        }
                                        break;
                                    case 1:
                                        /*double allPrice = Double.parseDouble(tv_all_price.getText().toString());
                                        double price1 = Double.parseDouble(singleAllocationBean.getPrice());
                                        double oPrice = Double.parseDouble(singleAllocationBean.getOprice());
                                        double opricenums = Double.parseDouble(MyApplication.getInstance().getSingleAllocationBean().getCate_info().get(max_position).getData().get(position).getGoods_price());
                                        double ogoodsnums = Double.parseDouble(MyApplication.getInstance().getSingleAllocationBean().getCate_info().get(max_position).getData().get(position).getGoods_nums());

                                        double deletePrice = Double.parseDouble(singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_price());
                                        String isadd = singleAllocationBean.getCate_info().get(max_position).getData().get(position).getIs_old();
                                        double goodsnums = Double.parseDouble(singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_nums());
                                        DecimalFormat df = new DecimalFormat("######0.00");
                                        double getPrice = 0.00;
                                        Log.e("要删除的价格", "" + deletePrice);
                                        if ("1".equals(isadd)) {
                                            if (deletePrice >= opricenums) {
                                                double delchajia = deletePrice - opricenums;
                                                Log.e("要删除的差价", "" + delchajia);
                                                if (goodsnums >= ogoodsnums) {
                                                    double a = (opricenums * sysnum + delchajia) * ogoodsnums;
                                                    double b = (opricenums + delchajia) * (goodsnums - ogoodsnums);
                                                    getPrice = allPrice - a - b;
                                                    Log.e("删除后的价格1", "" + getPrice);
                                                    Log.e("删除后的价格a", "" + a);
                                                    Log.e("删除后的价格b", "" + b);
                                                    Log.e("现价b", "" + goodsnums);
                                                    Log.e("原价b", "" + ogoodsnums);
                                                } else {
                                                    getPrice = allPrice - (opricenums * sysnum + delchajia) * goodsnums;
                                                    Log.e("删除后的价格2", "" + getPrice);
                                                }
                                            } else {
                                                if (goodsnums >= opricenums) {
                                                    getPrice = allPrice - deletePrice * sysnum * ogoodsnums - deletePrice * (goodsnums - ogoodsnums);
                                                    Log.e("删除后的价格3", "" + getPrice);
                                                } else {
                                                    getPrice = allPrice - deletePrice * sysnum * goodsnums;
                                                    Log.e("删除后的价格4", "" + getPrice);
                                                }
                                            }


//                                            if (replaceprice == 0.00){
//                                                getPrice = allPrice - deletePrice*sysnum*goodsnums;
//                                            }else{
//                                                getPrice = allPrice  - sysnum * deletePrice*goodsnums;
//                                            }
                                            double f1 = getdoublePrice(getPrice);
                                            if (f1 > 0) {
                                                tv_all_price.setText(df.format(f1) + "");
                                            } else {
                                                tv_all_price.setText("0.00");
                                            }
                                            MyApplication.getInstance().setPackagePrice(tv_all_price.getText().toString());

                                        } else {

                                            double price = allPrice - deletePrice * goodsnums;
                                            Log.e("删除后的价格4", "" + price);
                                            double f2 = getdoublePrice(price);
                                            if (f2 > 0) {
                                                tv_all_price.setText(df.format(f2) + "");
                                            } else {
                                                tv_all_price.setText("0.00");
                                            }
                                            MyApplication.getInstance().setPackagePrice(tv_all_price.getText().toString());
                                        }
                                        Log.e("数组的数量2", "" + singleAllocationBean.getCate_info().get(max_position).getData().size());
                                        Log.e("数组的数量1", "" + singleAllocationBean.getCate_info().size());*/
                                        if (MyUtils.isFastClick()) {
                                            packageDiyDel(max_position, position,true);
                                        }
                                        break;

                                    default:
                                        break;
                                }
                            }
                        });


                        my_listView.setAdapter(mAdapter);
                    } else {
                        String info = jsonObject.optString("info");
                        MyToast(info);
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                mAdapter.notifyDataSetChanged();
            }


            @Override
            public void onFinished() {
                findViewById(R.id.submit).setOnClickListener(SingleAllocationActivity.this);
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    //删除套餐(之前是客服端计算价格，现交给后台接口)
    private void packageDiyDel(final int max_position, final int position, final boolean delALl) {
        final String oGoodsId = singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_id();
        showDialog();
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("token", MyApplication.getInstance().getUserBean().getToken());
        } catch (Exception e) {
            return;
        }
        map.put("nowprice", Prefer.getInstance().getMarketPrice());//套餐的最新总价
        map.put("bathid", singleAllocationBean.getPackage_id());//套餐的id
        map.put("goodsid", singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_id());//删除商品的id
        map.put("skuid", singleAllocationBean.getCate_info().get(max_position).getData().get(position).getSku_id());//删除商品的skuid
        double count = 0;//删除前的个数
        double deleteNum = Double.parseDouble(singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_nums());//要删除的个数

        List<AllocationBean> data = singleAllocationBean.getCate_info().get(max_position).getData();
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getGoods_id().equals(oGoodsId)) {
                count = Double.parseDouble(data.get(i).getGoods_nums()) + count;
            }
        }

//        if (delALl) {
//            map.put("num", 0);//删除后的数量
//        } else {
//
//        }
        map.put("num",MyUtils.doubleRound(count - deleteNum));
        map.put("onum", MyUtils.doubleRound(count));//删除前的数量
        Set<String> set = map.keySet();
        for (String s : set) {
            Log.d(TAG, s+" : "+map.get(s));
        }
        XUtil.Post(Url.PACKAGE_PACKAGEDIYDEL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    Log.e("全删的结果: ", result);
                    JSONObject jsonObject = new JSONObject(result);
                    singleAllocationBean.setPrice(jsonObject.optString("info"));
                    Prefer.getInstance().setMarketPrice(jsonObject.optString("info"));
                    tv_all_price.setText(getPriceDoule(singleAllocationBean.getPrice()));
                    if(delALl){
                        singleAllocationBean.getCate_info().get(max_position).getData().remove(position);
                    } else {
                        double num = Double.parseDouble(singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_nums());
                        singleAllocationBean.getCate_info().get(max_position).getData().get(position).setGoods_nums(String.valueOf(num - 1));
                    }

                    List<AllocationBean> data2 = singleAllocationBean.getCate_info().get(max_position).getData();
                    for (int i = 0; i < data2.size(); i++) {
                        if (data2.get(i).getGoods_id().equals(oGoodsId)){
                            singleAllocationBean.getCate_info().get(max_position).getData().get(i).setGoods_price(jsonObject.optString("price"));
                        }
                    }

                    mAdapter.notifyDataSetChanged();
                    SingleAllocationActivity.this.mAdapter.notifyDataSetChanged();
                    MyApplication.getInstance().setSingleAllocationBean(singleAllocationBean);
                    closeDialog();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                int reduce = 0;
                Prefer.getInstance().setTestReduce(reduce);
                super.onFinished();
            }
        });
    }

    //删除套餐(之前是客服端计算价格，现交给后台接口) 减号的删除
    private void packageDiyDel2(final int max_position, final int position, final boolean delALl) {
        showDialog();
        final String oGoodsId = singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_id();
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("token", MyApplication.getInstance().getUserBean().getToken());
        } catch (Exception e) {
            return;
        }
        map.put("nowprice", Prefer.getInstance().getMarketPrice());//套餐的最新总价 tv_all_price.getText().toString()
        map.put("bathid", singleAllocationBean.getPackage_id());//套餐的id
        map.put("goodsid", singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_id());//删除商品的id
        map.put("skuid", singleAllocationBean.getCate_info().get(max_position).getData().get(position).getSku_id());//删除商品的skuid
        double count = 0;//删除前的个数

        List<AllocationBean> data = singleAllocationBean.getCate_info().get(max_position).getData();
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getGoods_id().equals(singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_id())) {
                count = Double.parseDouble(data.get(i).getGoods_nums()) + count;
            }
        }

//        if (delALl) {
//            map.put("num", 0);//删除后的数量
//        } else {
//
//        }
        map.put("num", MyUtils.doubleRound(count - 1));
        map.put("onum", MyUtils.doubleRound(count));//删除前的数量
        Set<String> set = map.keySet();
        for (String s : set) {
            Log.d(TAG, s+" : "+map.get(s));
        }
        XUtil.Post(Url.PACKAGE_PACKAGEDIYDEL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "返回的结果："+ result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    singleAllocationBean.setPrice(jsonObject.optString("info"));
                    Prefer.getInstance().setMarketPrice(jsonObject.optString("info"));
                    tv_all_price.setText(getPriceDoule(singleAllocationBean.getPrice()));
                    if(delALl){
                        singleAllocationBean.getCate_info().get(max_position).getData().remove(position);
                    } else {
                        double num = Double.parseDouble(singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_nums());
                        singleAllocationBean.getCate_info().get(max_position).getData().get(position).setGoods_nums(MyUtils.doubleRound(num - 1) + "");
                    }

                    List<AllocationBean> data2 = singleAllocationBean.getCate_info().get(max_position).getData();
                    for (int i = 0; i < data2.size(); i++) {
                        if (data2.get(i).getGoods_id().equals(oGoodsId)){
                            singleAllocationBean.getCate_info().get(max_position).getData().get(i).setGoods_price(jsonObject.optString("price"));
                        }
                    }

                    SingleAllocationActivity.this.mAdapter.notifyDataSetChanged();
                    MyApplication.getInstance().setSingleAllocationBean(singleAllocationBean);
                    SingleAllocationActivity.this.mAdapter.notifyDataSetChanged();
                    closeDialog();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                int reduce = 0;
                Prefer.getInstance().setTestReduce(reduce);
                super.onFinished();
            }
        });
    }

     private String getPriceDoule(String price){

       Double doubles= Double.parseDouble(price);

         DecimalFormat df = new DecimalFormat("#.00");
       return   df.format(doubles);


     }
    //编辑的删除套餐(之前是客服端计算价格，现交给后台接口) 减号的删除
    private void packageDiyEditDel(final int max_position, final int position, final boolean delALl, double onum, final double nowNum) {
        showDialog();
        final String oGoodsId = singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_id();
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("token", MyApplication.getInstance().getUserBean().getToken());
        } catch (Exception e) {
            return;
        }
        map.put("nowprice", Prefer.getInstance().getMarketPrice());//套餐的最新总价 tv_all_price.getText().toString()
        map.put("bathid", singleAllocationBean.getPackage_id());//套餐的id
        map.put("goodsid", singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_id());//删除商品的id
        map.put("skuid", singleAllocationBean.getCate_info().get(max_position).getData().get(position).getSku_id());//删除商品的skuid

        map.put("num", nowNum);
        map.put("onum", onum);//删除前的数量

        Set<String> set = map.keySet();
        for (String s : set) {
            Log.d(TAG, s+" : "+map.get(s));
        }

        XUtil.Post(Url.PACKAGE_PACKAGEDIYDEL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG,"- 返回的结果：" + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    singleAllocationBean.setPrice(jsonObject.optString("info"));
                    Prefer.getInstance().setMarketPrice(jsonObject.optString("info"));
                    tv_all_price.setText(getPriceDoule(singleAllocationBean.getPrice()));
                    MyApplication.getInstance().setPackagePrice(tv_all_price.getText().toString());
                    if(delALl){
                        singleAllocationBean.getCate_info().get(max_position).getData().remove(position);
                    } else {
                        double num = Double.parseDouble(singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_nums());
                        singleAllocationBean.getCate_info().get(max_position).getData().get(position).setGoods_nums(nowNum + "");
                    }

                    List<AllocationBean> data2 = singleAllocationBean.getCate_info().get(max_position).getData();
                    for (int i = 0; i < data2.size(); i++) {
                        if (data2.get(i).getGoods_id().equals(oGoodsId)){
                            singleAllocationBean.getCate_info().get(max_position).getData().get(i).setGoods_price(jsonObject.optString("price"));
                        }
                    }

                    SingleAllocationActivity.this.mAdapter.notifyDataSetChanged();
                    MyApplication.getInstance().setSingleAllocationBean(singleAllocationBean);
                    SingleAllocationActivity.this.mAdapter.notifyDataSetChanged();
                    closeDialog();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                int reduce = 0;
                Prefer.getInstance().setTestReduce(reduce);
                super.onFinished();
            }
        });
    }


    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                showDialogv7("未添加至购物车，确认返回后将被清空！", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        list.clear();
                        valueList.clear();
                        finish();
                    }
                });
                break;

            //下一步
            case R.id.submit:
                List<SingleItemBean> arrayList = new ArrayList<>();
                list1 = new ArrayList<>();
//                Log.e("==数据集合 cateList ===", "" + cateList.size() + " ============= " + cateList);
                for (SingleItemBean singleItemBean : cateList) {
                    for (AllocationBean allocationBean : singleItemBean.getData()) {
//                        Log.e("==数据集合 遍历 1  ===", "进来了");
                        Double num = Double.parseDouble(allocationBean.getGoods_nums().toString());
//                        Log.e("==数据数量大小 num ===", "进来了====" + num);
                        if (num > 0) {
//                            Log.e("==数据集合 遍历 2  ===", "进来了");
                            list1.add(allocationBean);
                        }
                    }

                    if (list1 != null) {
                        SingleItemBean itemBean = new SingleItemBean();
                        itemBean.setCate_id(singleItemBean.getCate_id());
                        itemBean.setCate_name(singleItemBean.getCate_name());
                        itemBean.setCount(singleItemBean.getCount());
                        itemBean.setData(list1);
                        arrayList.add(itemBean);
//                      Log.e("==数据集合 arrayList 里 ===", "" + arrayList.size() + " ============= " + arrayList);
                    }
                }

//                Log.e("==数据集合 arrayList 外 ===", "" + arrayList.size() + " ============= " + arrayList);
                EntiretyShoppingMolder molder = new EntiretyShoppingMolder();
                List<EntiretyMolder1> data = new ArrayList<>();
                for (SingleItemBean itemBean : arrayList) {
                    entiretyMolder1 = new EntiretyMolder1();
                    entiretyMolder1.setCate_id(itemBean.getCate_id());
                    List<EntiretyGoodsMolder2> data2 = new ArrayList<>();
                    for (AllocationBean allocationBean : itemBean.getData()) {
                        EntiretyGoodsMolder2 goodsMolder2 = new EntiretyGoodsMolder2();
                        goodsMolder2.setGoods_id(allocationBean.getGoods_id());
                        goodsMolder2.setGoods_nums(allocationBean.getGoods_nums());
                        goodsMolder2.setSku_id(allocationBean.getSku_id());
                        data2.add(goodsMolder2);
                    }
                    entiretyMolder1.setData(data2);
                }
                data.add(entiretyMolder1);

//                Log.e("==数据集合  data ===", "" + data.size() + " ============= " + data);
                String Amoney = tv_all_price.getText().toString();
                molder.setData(data);
                molder.setPackage_id(packageId);
                molder.setColor_id(colorId);
                molder.setSku_id(skuId);
                molder.setPrice(Amoney);
                intent = new Intent(this, BashRoomListActivity.class);//BashRoomListActivity  AddCartActivity
                intent.putExtra("molder", molder);
                intent.putExtra("money", Amoney);
                intent.putExtra("packageId",packageId);
                intent.putExtra("source","A");
                if (!TextUtils.isEmpty(goods_name)) {
                    intent.putExtra("goods_name",goods_name);
                }
                startActivity(intent);


                break;

            //新增主体
            case R.id.tv_add:
                int position = (int) v.getTag();
                intent = new Intent(this, AddedWashActivity.class);
                intent.putExtra("position", position);
                intent.putExtra("packageId", packageId);
                intent.putExtra("cateId", singleAllocationBean.getCate_info().get(position).getCate_id());
                startActivityForResult(intent, ADDEDWASH);
                break;

            //重置
            case R.id.tv_reset:
                showDialogv7("您确定要重置吗?", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (list1 != null) {
                            list1.clear();
                        }
                        valueList.clear();
                        upload();
                    }
                });
                break;

            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REPLACE://替换
                if (resultCode == RESULT_OK) {
//                    //拿过来的值
//                    int max_position = data.getExtras().getInt("max_position", 0);
//                    int position = data.getExtras().getInt("position", 0);
//                    String price = data.getExtras().getString("price");
//                    Log.e("返回来的价格= price == ", "" + price);
//                    AllocationBean bean = (AllocationBean) data.getSerializableExtra("data");
//                    bean.setOldPrice(replaceprice);
//                    //把原来的移除掉
//                    singleAllocationBean.getCate_info().get(max_position).getData().remove(position);
//                    //把拿到的添加到集合中去
//                    singleAllocationBean.getCate_info().get(max_position).getData().add(position, bean);
//                    double oldPrice = Double.parseDouble(bean.getOldPrice());  //最初要被替换的商品价格
//                    Log.e("最初商品价格= oldPrice == ", "" + oldPrice);
//                    double allPrice = Double.parseDouble(tv_all_price.getText().toString());
//                    Log.e("= 商品总价格 = allPrice == ", "" + allPrice);
//                    double price1 = Double.parseDouble(singleAllocationBean.getPrice());
//                    Log.e("= 总商品 价格 = price1 == ", "" + price1);
//                    double oPrice = Double.parseDouble(singleAllocationBean.getOprice());
//                    Log.e("= 总商品 原价 = oPrice == ", "" + oPrice);
//                    currentPrice = singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_price();
//                    double goodsPrice = Double.parseDouble(currentPrice);
//                    Log.e("=当前商品价格 = goodsPrice = ", "" + goodsPrice);
//                    double newPrice = Double.parseDouble(bean.getGoods_price());
//                    Log.e("拿回来的商品价格 changePrice ==", "" + newPrice);
//                    String isoldd = singleAllocationBean.getCate_info().get(max_position).getData().get(position).getIs_old();
//                    Log.e("=是否是老的= isadd == ", "" + isoldd);
//                    double li = price1 / oPrice;
//                    double bili = getdoublePrice(li);
//                    Log.e("=比例= bili == ", "" + bili);
//
//                    double endPrice;
//                    double end;
//                    if ("1".equals(isoldd)) {
//                        //判断拿过来的商品与被要替换的商品价格对比
//                        if (newPrice >= oldPrice) {  //新的大与老的  直接加上差价
//                            if(allPrice >= price1){
//                                allPrice = allPrice - (goodsPrice - oldPrice) * bili;
//                            }
//
//                            end = newPrice - oldPrice;
//                            Log.e("=差价 = 大于 = end == ", "" + end);
//
//                            double eprice = allPrice + end;
//
//                            endPrice = getdoublePrice(eprice);
//                            Log.e("=最终商品价格==大于== ", "" + endPrice);
//
//                        } else {
//                            allPrice = allPrice - (goodsPrice - oldPrice);
//                            //新的小于老的价格时， 用总价格 - 0.8 * 差价；
//                            end = bili * (oldPrice - newPrice);
//                            Log.e("=差价 = 小于 = end == ", "" + end);
//
//                            double getPrice = allPrice - end;
//
//                            endPrice = getdoublePrice(getPrice);
//                            Log.e("=最终商品价格==小于== ", "" + endPrice);
//                        }
//                    } else {
//                        if (newPrice >= oldPrice) {
//                            double price11 = newPrice - oldPrice;
//                            double getPrice = allPrice + price11;
//                            endPrice = getdoublePrice(getPrice);
//                        } else {
//                            end = oldPrice - newPrice;
//                            double eprice = allPrice - end;
//                            endPrice = getdoublePrice(eprice);
//                        }
//
//                    }
//
//                    tv_all_price.setText(endPrice + "");
//                    mAdapter.notifyDataSetChanged()
                    if (MyUtils.isFastClick()) {
                        int max_position = data.getIntExtra("max_position", 0);
                        int position = data.getIntExtra("position", 0);
                        AllocationBean bean = (AllocationBean) data.getSerializableExtra("data");
                        Log.v(TAG,"new good==="+bean.getGoods_id());
                        AllocationBean allocationBean = a2d(bean);
                        packageDiyReplace(max_position, position, bean);
                    }

                    /*double beprice = Double.parseDouble(singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_price());
                    double benums = MyApplication.getInstance().getYsnum();

                    double allPrice = Double.parseDouble(tv_all_price.getText().toString());
                    Log.e("新总价价格=allPrice=", "" + allPrice);
                    double price1 = Double.parseDouble(singleAllocationBean.getPrice());
                    double oPrice = Double.parseDouble(singleAllocationBean.getOprice());
                    double xishu = price1 / oPrice;
                    changePrice = Double.parseDouble(singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_price());
                    String changePriceid = singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_id();
                    Log.e("改变价格 = changePrice = ", "" + changePrice);
                    String isadd = singleAllocationBean.getCate_info().get(max_position).getData().get(position).getIs_old();

                    String oldprice = MyApplication.getInstance().getSingleAllocationBean().getCate_info().get(max_position).getData().get(position).getGoods_price();
                    Double oldpricenum = Double.parseDouble(oldprice);
                    Double packageprice = Double.parseDouble(MyApplication.getInstance().getPackagePrice());
                    Log.e("原价", "" + oldpricenum);
                    Log.e("被替換價格", "" + replaceprice);
                    Log.e("替換價格", "" + changePrice);
                    Log.e("替換之前的價格", "" + beprice);
                    Log.e("替换的标志", MyApplication.getInstance().getSingleAllocationBean().getCate_info().get(max_position).getData().get(position).getIs_old());
                    if (MyApplication.getInstance().getSingleAllocationBean().getCate_info().get(max_position).getData().get(position).getIs_old().equals("1")) {
                        if (changePrice >= beprice) {
//                            if (beprice >= oldpricenum) {
                            if (oldpricenum >= beprice) {
                                Double chajia = beprice - oldpricenum;
                                Log.e("差价1", "" + chajia);
//                                all_endPrice = packageprice - (chajia + beprice) * benums + changePrice * benums;

                                all_endPrice = packageprice - beprice * benums + changePrice * benums;
                                MyApplication.getInstance().setPackagePrice("" + all_endPrice);
                            } else {
                                Double chajia2 = changePrice - oldpricenum;
                                Double chajia_3 = oldpricenum - beprice;

                                Double chajia2_2 = chajia2 * benums;
                                Double chajia_3_2 = chajia_3 * benums * sysnum;
                                Log.e("差价1_1", "" + chajia2);
                                Log.e("差价1_2", "" + chajia_3);
                                Log.e("差价2_2", "" + chajia2_2);
                                Log.e("差价3_2", "" + chajia_3_2);
                                all_endPrice = packageprice + chajia_3_2 + chajia2_2;
                                MyApplication.getInstance().setPackagePrice("" + all_endPrice);
                            }
                        } else {
//                            if (beprice >=  oldpricenum) {
                            if (oldpricenum >= beprice) {
                                Double chajia = beprice - oldpricenum;
                                Log.e("差价2", "" + chajia);
                                all_endPrice = packageprice - chajia * benums - (oldpricenum - changePrice) * benums * sysnum;
                                MyApplication.getInstance().setPackagePrice("" + all_endPrice);
                            } else {
                                Double chajia2 = oldpricenum - beprice;
                                all_endPrice = packageprice + chajia2 * benums * sysnum - (oldpricenum - changePrice) * benums * sysnum;
                                MyApplication.getInstance().setPackagePrice("" + all_endPrice);
                            }

                        }
                    } else {
                        all_endPrice = packageprice - beprice * benums + changePrice * benums;
                        Log.e("套餐价", "" + packageprice);
                        Log.e("最初的价格", "" + beprice * benums);
                        Log.e("替换价格", "" + changePrice * benums);
                        Log.e("新增替换", "" + all_endPrice);
                        MyApplication.getInstance().setPackagePrice("" + all_endPrice);
                    }

//                    if (replaceprice - changePrice != 0){
//                        if(oldpricenum - changePrice >= 0){//现在商品的价格大于替换价格
//                            Double chajia = oldpricenum - changePrice;
//                            Log.e("差价",""+(chajia*sysnum));
//                            all_endPrice = packageprice - (chajia * sysnum);
//                            MyApplication.getInstance().setPackagePrice(""+all_endPrice);
//                        }else if (oldpricenum - changePrice < 0){
//                            all_endPrice = packageprice + (changePrice-oldpricenum);
//                            MyApplication.getInstance().setPackagePrice(""+all_endPrice);
//                        }
//                    }
                    DecimalFormat df = new DecimalFormat("######0.00");*/

                }
                break;

            case ADDEDWASH://新增
                if (resultCode == RESULT_OK) {
                    if (MyUtils.isFastClick()) {
                        Log.e("新增", "-------------------");
                        int position = data.getIntExtra("position", 0);
                        AllocationBean bean = (AllocationBean) data.getSerializableExtra("data");
                        Log.e("数组的数量2", "" + singleAllocationBean.getCate_info().get(position).getData().size());
                        Log.e("数组的数量1", "" + singleAllocationBean.getCate_info().size());
//                    MyApplication.getInstance().getSingleAllocationBean().getCate_info().get(position).getData().add(bean);

                        Log.e("-  新增 --", singleAllocationBean.getCate_info().get(position).getData().size() + "");

                    /*double allPrice = Double.parseDouble(tv_all_price.getText().toString());
                    double addPrice = Double.parseDouble(bean.getGoods_price() + "");
                    double price = addPrice + allPrice;
                    double f2 = getdoublePrice(price);
                    tv_all_price.setText(f2 + "");*/
                        packageDiyAdd(position, bean);
                    }
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


    //新增套餐(之前是客服端计算价格，现交给后台接口) 单个的加
    private void packageDiyAdd2(final int position, final AllocationBean bean) {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("token", MyApplication.getInstance().getUserBean().getToken());
        } catch (Exception e) {
            return;
        }
        map.put("nowprice", Prefer.getInstance().getMarketPrice());//tv_all_price.getText().toString()
        map.put("bathid", singleAllocationBean.getPackage_id());
        map.put("goodsid", bean.getGoods_id());
        map.put("skuid", bean.getSku_id());
        double count = 0;

        List<AllocationBean> data = singleAllocationBean.getCate_info().get(position).getData();
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getGoods_id().equals(bean.getGoods_id())) {
                count = Double.parseDouble(data.get(i).getGoods_nums());
                if (!TextUtils.isEmpty(data.get(i).getSku_id())) {
                    if(data.get(i).getSku_id().equals(bean.getSku_id())){
                        index2 = i;
                    }else {
                        Log.e("skuid: 111" , data.get(i).getSku_id() + " : " + bean.getSku_id());
                    }
                }else {
                    index2 = i;
                }
                Log.e("skuid: 222" , data.get(i).getSku_id() + " : " + bean.getSku_id() + "index2: " + index2);
            }
        }
        map.put("num", MyUtils.doubleRound(count + 1));
        map.put("onum", MyUtils.doubleRound(count));
        Set<String> set = map.keySet();
        for (String s : set) {
            Log.d(TAG, s+" : "+map.get(s));
        }
        XUtil.Post(Url.PACKAGE_PACKAGEDIYADD, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    singleAllocationBean.setPrice(jsonObject.optString("info"));
                    Prefer.getInstance().setMarketPrice(jsonObject.optString("info"));
                    tv_all_price.setText(getPriceDoule(singleAllocationBean.getPrice()));
//                    if (index2 == -1) {
                    if (!isListContained(bean, position)) {
                        singleAllocationBean.getCate_info().get(position).getData().add(bean);
                        Log.e("skuid: 333" , " : " + bean.getSku_id() + "index2: " + index2);
                    } else {
                        double num = Double.parseDouble(singleAllocationBean.getCate_info().get(position).getData().get(index2).getGoods_nums());
                        singleAllocationBean.getCate_info().get(position).getData().get(index2).setGoods_nums(String.valueOf(num + 1));
                    }
                    index2 = -1;

                    List<AllocationBean> datas = singleAllocationBean.getCate_info().get(position).getData();
                    for (int i = 0; i <datas.size() ; i++) {
                        if (!TextUtils.isEmpty(bean.getSku_id())) {
                            if (bean.getGoods_id().equals(datas.get(i).getGoods_id()) && bean.getSku_id().equals(datas.get(i).getSku_id())){
                                datas.get(i).setGoods_price(jsonObject.optString("price"));
                            }
                        }else {
                            if (bean.getGoods_id().equals(datas.get(i).getGoods_id())){
                                datas.get(i).setGoods_price(jsonObject.optString("price"));
                            }
                        }
                    }

                    MyApplication.getInstance().setSingleAllocationBean(singleAllocationBean);
                    mAdapter.notifyDataSetChanged();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                closeDialog();
                int tt= 0;
                Prefer.getInstance().setTest(tt);

                super.onFinished();
            }
        });
    }

    //编辑的新增套餐(之前是客服端计算价格，现交给后台接口) 单个的加
    private void packageDiyEditAdd(final int position, final AllocationBean bean,double onum,double nowNum) {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("token", MyApplication.getInstance().getUserBean().getToken());
        } catch (Exception e) {
            return;
        }
        map.put("nowprice", Prefer.getInstance().getMarketPrice());//tv_all_price.getText().toString()
        map.put("bathid", singleAllocationBean.getPackage_id());
        map.put("goodsid", bean.getGoods_id());
        map.put("skuid", bean.getSku_id());

        map.put("num", nowNum);
        map.put("onum", onum);
        Set<String> set = map.keySet();
        for (String s : set) {
            Log.d(TAG, s+" : "+map.get(s));
        }
        XUtil.Post(Url.PACKAGE_PACKAGEDIYADD, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG,"+ 返回的结果：" + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    singleAllocationBean.setPrice(jsonObject.optString("info"));
                    Prefer.getInstance().setMarketPrice(jsonObject.optString("info"));
                    tv_all_price.setText(getPriceDoule(singleAllocationBean.getPrice()));
                    MyApplication.getInstance().setPackagePrice(singleAllocationBean.getPrice());
                    if (!isListContained(bean, position)) {
                        singleAllocationBean.getCate_info().get(position).getData().add(bean);
                        Log.e("skuid: 333" , " : " + bean.getSku_id() + "index2: " + index2);
                    } else {
                        double num = Double.parseDouble(singleAllocationBean.getCate_info().get(position).getData().get(index2).getGoods_nums());
                        singleAllocationBean.getCate_info().get(position).getData().get(index2).setGoods_nums(String.valueOf(num + 1));
                    }
                    index2 = -1;

                    List<AllocationBean> datas = singleAllocationBean.getCate_info().get(position).getData();
                    for (int i = 0; i <datas.size() ; i++) {
                        if (!TextUtils.isEmpty(bean.getSku_id())) {
                            if (bean.getGoods_id().equals(datas.get(i).getGoods_id()) && bean.getSku_id().equals(datas.get(i).getSku_id())){
                                datas.get(i).setGoods_price(jsonObject.optString("price"));
                            }
                        }else {
                            if (bean.getGoods_id().equals(datas.get(i).getGoods_id())){
                                datas.get(i).setGoods_price(jsonObject.optString("price"));
                            }
                        }
                    }

                    MyApplication.getInstance().setSingleAllocationBean(singleAllocationBean);
                    mAdapter.notifyDataSetChanged();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                closeDialog();
                int tt= 0;
                Prefer.getInstance().setTest(tt);

                super.onFinished();
            }
        });
    }


    private boolean isListContained(AllocationBean bean, int position){
        boolean contained  =false;
        List<AllocationBean> data = singleAllocationBean.getCate_info().get(position).getData();
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getGoods_id().equals(bean.getGoods_id())) {
                if (!TextUtils.isEmpty(data.get(i).getSku_id())) {
                    if(data.get(i).getSku_id().equals(bean.getSku_id())){
                        index2 = i;
                        contained = true;
                    }else {
                        Log.e("skuid: 111" , data.get(i).getSku_id() + " : " + bean.getSku_id());
                    }
                }else {
                    index2 = i;
                    contained = true;
                }
                Log.e("skuid: 222" , data.get(i).getSku_id() + " : " + bean.getSku_id() + "index2: " + index2);
            }
        }
        return contained;
    }


    int index = -1;
    //新增套餐(之前是客服端计算价格，现交给后台接口)
    private void packageDiyAdd(final int position, final AllocationBean bean) {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("token", MyApplication.getInstance().getUserBean().getToken());
        } catch (Exception e) {
            return;
        }
        map.put("nowprice", Prefer.getInstance().getMarketPrice());
        map.put("bathid", singleAllocationBean.getPackage_id());
        map.put("goodsid", bean.getGoods_id());
        map.put("skuid", bean.getSku_id());
        double count = 0;
        List<AllocationBean> data = singleAllocationBean.getCate_info().get(position).getData();
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getGoods_id().equals(bean.getGoods_id())) {
                count = Double.parseDouble(data.get(i).getGoods_nums());
                if (!TextUtils.isEmpty(data.get(i).getSku_id())) {
                    if(data.get(i).getSku_id().equals(bean.getSku_id())){
                        index = i;
                    }
                }else {
                    index = i;
                }
            }
        }
        map.put("num", MyUtils.doubleRound (count + 1));
        map.put("onum", MyUtils.doubleRound(count));
        Set<String> set = map.keySet();
        for (String s : set) {
            Log.d(TAG, s+" : "+map.get(s));
        }
        XUtil.Post(Url.PACKAGE_PACKAGEDIYADD, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    singleAllocationBean.setPrice(jsonObject.optString("info"));
                    Prefer.getInstance().setMarketPrice(jsonObject.optString("info"));
                    if (index == -1) {
                        singleAllocationBean.getCate_info().get(position).getData().add(bean);
                    } else {
                        double num = Double.parseDouble(singleAllocationBean.getCate_info().get(position).getData().get(index).getGoods_nums());
                        singleAllocationBean.getCate_info().get(position).getData().get(index).setGoods_nums(String.valueOf(num + 1));
                    }
                    index = -1;
                    tv_all_price.setText(getPriceDoule(singleAllocationBean.getPrice()));

                    List<AllocationBean> datas = singleAllocationBean.getCate_info().get(position).getData();
                    for (int i = 0; i <datas.size() ; i++) {
                        if (!TextUtils.isEmpty(bean.getSku_id())) {
                            if (bean.getGoods_id().equals(datas.get(i).getGoods_id()) && bean.getSku_id().equals(datas.get(i).getSku_id())){
                                datas.get(i).setGoods_price(jsonObject.optString("price"));
                            }
                        }else {
                            if (bean.getGoods_id().equals(datas.get(i).getGoods_id())){
                                datas.get(i).setGoods_price(jsonObject.optString("price"));
                            }
                        }
                    }

                    MyApplication.getInstance().setSingleAllocationBean(singleAllocationBean);
                    mAdapter.notifyDataSetChanged();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                closeDialog();
                int tt= 0;
                Prefer.getInstance().setTest(tt);
                super.onFinished();
            }
        });
    }

    //替换套餐(之前是客服端计算价格，现交给后台接口)
    private void packageDiyReplace(final int max_position, final int position, final AllocationBean bean) {
        System.out.println("max_position==="+max_position);

        if (bean.getGoods_id()
                .equals(singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_id())){
            // 相同商品
            if (TextUtils.isEmpty(bean.getSku_id())){
                Toast.makeText(SingleAllocationActivity.this, "请选择不同规格商品替换", Toast.LENGTH_SHORT).show();
                return;
            } else{
                if (bean.getSku_id().equals(singleAllocationBean.getCate_info().get(max_position).getData().get(position).getSku_id())){
                    Toast.makeText(SingleAllocationActivity.this, "请选择不同规格商品替换", Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        }
        showDialog();
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("token", MyApplication.getInstance().getUserBean().getToken());
        } catch (Exception e) {
            return;
        }
        map.put("nowprice", Prefer.getInstance().getMarketPrice());
        map.put("cbathid", singleAllocationBean.getPackage_id());
        map.put("cgoodsid", singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_id());
        map.put("cskuid", singleAllocationBean.getCate_info().get(max_position).getData().get(position).getSku_id());
        final String cGoodId = singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_id();
        final  String cSkuId = singleAllocationBean.getCate_info().get(max_position).getData().get(position).getSku_id();
        double count = 0;
        double iCount = 0;
        double afterCount = 0;


        List<AllocationBean> data = singleAllocationBean.getCate_info().get(max_position).getData();
        for (int i = 0; i < data.size() ; i++) {
            if (bean.getGoods_id().equals(data.get(i).getGoods_id())){
                if (!TextUtils.isEmpty(bean.getSku_id()) && bean.getSku_id().equals(data.get(i).getSku_id())) {
                    afterCount ++;
                }
            }
        }
        for (int i = 0; i < data.size(); i++) {

            if (data.get(i).getGoods_id()
                    .equals(singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_id())) {
                count += Double.parseDouble(data.get(i).getGoods_nums());
            }
        }

        map.put("cpnum", count - Double.parseDouble(bean.getGoods_nums()));
        map.put("cnum", count);

        map.put("ibathid", singleAllocationBean.getPackage_id());
        map.put("igoodsid", bean.getGoods_id());
        map.put("iskuid", bean.getSku_id());
//
            List<AllocationBean> data1 = singleAllocationBean.getCate_info().get(max_position).getData();
            for (int i = 0; i < data1.size(); i++) {

                if (data1.get(i).getGoods_id().equals(bean.getGoods_id())) { // 标配 标配
                    if (!TextUtils.isEmpty(data1.get(i).getSku_id())) { // 顶 侧
                       // 和新商品是同一件
                        if (data1.get(i).getSku_id().equals(bean.getSku_id())) {
                            // 规格相同 添加数量
                            iCount += Double.parseDouble(data1.get(i).getGoods_nums());
                        } else {
                            iCount += Double.parseDouble(data1.get(i).getGoods_nums());
                        }
                    } else {
                     // 无规格
                        if (TextUtils.isEmpty(bean.getSku_id())) {
                            iCount += Double.parseDouble(data1.get(i).getGoods_nums());
                        }
                    }
                }
            }


        map.put("ipnum",afterCount);//ipnum 要替换的商品在配置清单修改之前的数量
        if (!bean.getGoods_id().equals(data.get(position).getGoods_id())) {
            map.put("inum", iCount + Double.parseDouble(bean.getGoods_nums()));
        } else {
            map.put("inum", iCount);
        }

        Set<String> set = map.keySet();
        for (String s : set) {
            Log.e("HMin66", s+" : "+map.get(s));
        }

        XUtil.Post(Url.PACKAGE_PACKAGEDIYREPLACE, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    Log.e(TAG,"替换返回的结果： "+ result);
                    System.out.println();
                    JSONObject jsonObject = new JSONObject(result);
                    if ("1".equals(jsonObject.optString("result"))) {
                        singleAllocationBean.setPrice(jsonObject.optString("info"));
                        Prefer.getInstance().setMarketPrice(jsonObject.optString("info"));
                        tv_all_price.setText(getPriceDoule(singleAllocationBean.getPrice()));

                        List<AllocationBean> data = singleAllocationBean.getCate_info().get(max_position).getData();
                        boolean isExist = false;
                        boolean isThMyself = false;//是否替换的是自己
                        List<AllocationBean> removeList = new ArrayList<>();
                     //   int tagNum = 0;//用来记录列表中 与 新替换来的 goods_id 相同的数量
                        for (int i = 0; i < data.size(); i++) {

//                            if(bean.getGoods_id().equals(data.get(i).getGoods_id()) ||singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_id().equals(data.get(i).getGoods_id())){
                            if(singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_id().equals(data.get(i).getGoods_id())){
                                removeList.add(data.get(i));
                            }
                            if (!bean.getGoods_id().equals(data.get(position).getGoods_id()) && bean.getGoods_id().equals(data.get(i).getGoods_id())){
                                if (((bean.getSku_id() == null) && (data.get(i).getSku_id() ==null)) || bean.getSku_id().equals(data.get(i).getSku_id())){
                                    removeList.add(data.get(i));
                                }
                            }
                            // 和新商品是同一类型
//                            if (data.get(i).getGoods_id().equals(bean.getGoods_id())) { // 标配 标配
//
//                                if (!TextUtils.isEmpty(data.get(i).getSku_id())||data.get(i).getSku_id()!=null||!TextUtils.isEmpty(bean.getSku_id())||bean.getSku_id()!=null) { // 顶 侧
//                                    // 和新商品是同一件
//                                    if (data.get(i).getSku_id().equals(bean.getSku_id())) { //false true
//                                        isExist = true;
//
////                                        if (i == position)
////                                            isThMyself = true;
////                                        if(!isThMyself) {
//                                        //data某个位置里面的数据和替换中的一样
//                                           //i 增加数值
//                                            double goodNums = Double.parseDouble(singleAllocationBean.getCate_info()
//                                                    .get(max_position).getData()
//                                                    .get(i).getGoods_nums());
//
//                                            singleAllocationBean.getCate_info()
//                                                    .get(max_position).getData()
//                                                    .get(i).setGoods_nums((goodNums + Double.parseDouble(bean.getGoods_nums())) + "");
//
//                                            //设置价格
//                                            singleAllocationBean.getCate_info().get(max_position).getData().
//                                                    get(i).setGoods_price(jsonObject.optString("add_price"));
//                                            //移除 positon
//                                        singleAllocationBean.getCate_info()
//                                                .get(max_position).getData()
//                                                .remove(position);
//                                  //      }
//
//
//                                        //存在
////                                        isExist = true;
////
////                                        if (i == position)
////                                            //自己
////                                        isThMyself = true;
////
////                                        double goodNums = Double.parseDouble(singleAllocationBean.getCate_info()
////                                                .get(max_position).getData()
////                                                .get(i).getGoods_nums());
////
////                                        singleAllocationBean.getCate_info()
////                                                .get(max_position).getData()
////                                                .get(i).setGoods_nums((goodNums + Double.parseDouble(bean.getGoods_nums())) + "");
////
////                                        singleAllocationBean.getCate_info().get(max_position).getData().
////                                                get(i).setGoods_price(jsonObject.optString("add_price"));
//                                    } else {
//                                        // 不同规格商品
//
//                                            isExist = true;
//
//                                        if (i == position)
//                                            isThMyself = true;
//                                        if(isThMyself) {
//                                            singleAllocationBean.getCate_info()
//                                                    .get(max_position).getData()
//                                                    .set(position, bean);
//                                            singleAllocationBean.getCate_info().get(max_position).getData().
//                                                    get(position).setGoods_price(jsonObject.optString("add_price"));
//                                        }
//
//                                    }
//                                } else {
//                                    double goodNums = Double.parseDouble(singleAllocationBean.getCate_info()
//                                            .get(max_position).getData()
//                                            .get(i).getGoods_nums());
//
//                                    singleAllocationBean.getCate_info()
//                                            .get(max_position).getData()
//                                            .get(i).setGoods_nums((goodNums + Double.parseDouble(bean.getGoods_nums())) + "");
//
//                                    //设置价格
//                                    singleAllocationBean.getCate_info().get(max_position).getData().
//                                            get(i).setGoods_price(jsonObject.optString("add_price"));
//                                    //移除 positon
//                                    singleAllocationBean.getCate_info()
//                                            .get(max_position).getData()
//                                            .remove(position);
//                                }
//                            }else {
//
//                                isExist=false;
//                                if (i == position)
////                                            //自己
//                                   isThMyself = true;
//                                if(isThMyself){
//                                    singleAllocationBean.getCate_info()
//                                            .get(max_position).getData()
//                                            .set(position, bean);
//                                    singleAllocationBean.getCate_info().get(max_position).getData().
//                                            get(position).setGoods_price(jsonObject.optString("add_price"));
//                                }
//
//                                Log.e(TAG,"不相同goodid不同skuid" +
//                                        data.get(i).getGoods_id() + " : " + bean.getGoods_id()
//                                + " skuid: " + data.get(i).getSku_id() + " : " + bean.getSku_id());
//
//
//
//                            }
                        }
                        double num = 0;
                        for(AllocationBean bean1:removeList){
//                            if() {
                                num += Double.valueOf(bean1.getGoods_nums());
//                            }
                        }
//                        num -= Double.valueOf(data.get(position).getGoods_nums());
//                        if (!bean.getGoods_id().equals(data.get(position).getGoods_id())){
////                            removeList.add(data.get(position));
//                            num+=Double.valueOf(data.get(position).getGoods_nums());
//                        }
                        bean.setGoods_nums(num+"");
                        bean.setGoods_price(jsonObject.optString("add_price"));
                        data.removeAll(removeList);
                        data.add(position, bean);
                        //
//                       if(isExist&&isThMyself){
//
//                       }
//                       if(isExist&&!isThMyself){
//                                                               double goodNums = Double.parseDouble(singleAllocationBean.getCate_info()
//                                            .get(max_position).getData()
//                                            .get(i).getGoods_nums());
//                                    singleAllocationBean.getCate_info()
//                                            .get(max_position).getData()
//                                            .get(i).setGoods_nums((goodNums + Double.parseDouble(bean.getGoods_nums())) + "");
//                                    singleAllocationBean.getCate_info().get(max_position).getData().
//                                            get(i).setGoods_price(jsonObject.optString("add_price"));
//                           singleAllocationBean.getCate_info()
//                                    .get(max_position).getData()
//                                    .remove(position);
//                       }
//                        if (isExist && isThMyself&&isdelete) {
//                            // 原本存在新商品 只要把旧商品删除
//                            singleAllocationBean.getCate_info()
//                                    .get(max_position).getData()
//                                    .remove(position);
//                        }
//
//                          if (!isExist && !isThMyself){
//
//                            singleAllocationBean.getCate_info()
//                                    .get(max_position).getData()
//                                    .set(position, bean);
//                            singleAllocationBean.getCate_info().get(max_position).getData().
//                                    get(position).setGoods_price(jsonObject.optString("add_price"));
//
//                        }

//                        int deletePosition = -1;//删除的位置
//                        int updatePosition = -1;//删除后更新的位置
//--------------------------------------------------------------------------------
//                        int size  = data.size();
//                        String gid = singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_id();
//                        int size2 = singleAllocationBean.getCate_info().get(max_position).getData().size();
//                        // 替换不同商品
//                        if (!bean.getGoods_id().equals(singleAllocationBean.getCate_info().get(max_position).getData().get(position).getGoods_id())) {
//                            // 删除替换的相同商品
//                            int replacePosition = -1; //存在替换商品的position
//                            for (int i = 0; i < size2; i++) {
//                                if (!TextUtils.isEmpty(bean.getSku_id())) {
//                                    if (bean.getGoods_id().equals(singleAllocationBean.getCate_info().get(max_position).getData().get(i).getGoods_id())
//                                            && bean.getSku_id().equals(singleAllocationBean.getCate_info().get(max_position).getData().get(i).getSku_id())) {
//                                        replacePosition = i;
//                                    }
//                                } else {
//                                    if (bean.getGoods_id().equals(singleAllocationBean.getCate_info().get(max_position).getData().get(i).getGoods_id())) {
//                                        replacePosition = i;
//                                    }
//                                }
//                            }
//
//                            if (replacePosition == -1) {
//                                // 不存在替换商品
//                                singleAllocationBean.getCate_info().get(max_position).getData().set(position, bean);
//                            } else {
//                                singleAllocationBean.getCate_info().get(max_position).getData().get(replacePosition).setGoods_nums(replaceNum + "");
//                            }
//
//                            // 删除被替换商品
//                            for (int i = 0; i < size; i++) {
//                                if (data.get(i).getGoods_id().equals(gid)) {
//                                    // 存在替换的相同商品
//                                    singleAllocationBean.getCate_info().get(max_position).getData().remove(i);
//                                    size--;
//                                    i--;
//                                }
//                            }
//                        } else {
//                            int replacePosition = -1; //存在替换商品的position
//                            // 替换的是相同商品 不同规格
//                            for (int i = 0; i < size; i++) {
//                                // 先判读 替换商品 规格是否存在
//                                if (!TextUtils.isEmpty(bean.getSku_id())) {
//                                    // 相同商品
//                                    if (bean.getGoods_id().equals(singleAllocationBean.getCate_info().get(max_position).getData().get(i).getGoods_id())){
//                                        if(bean.getSku_id().equals(singleAllocationBean.getCate_info().get(max_position).getData().get(i).getSku_id())) {
//                                            // 存在该商品
//                                            replacePosition = i;
//                                        }
//                                    }
//                                } else {
//                                    // 替换同种同规格商品 不做 任何操作
//                                }
//                            }
//
//                            if (replacePosition == -1) {
//                                // 不存在替换商品
//                                singleAllocationBean.getCate_info().get(max_position).getData().set(position, bean);
//                            } else {
//                                singleAllocationBean.getCate_info().get(max_position).getData().get(replacePosition).setGoods_nums(replaceNum + "");
//                                singleAllocationBean.getCate_info().get(max_position).getData().remove(position);
//                            }
//                        }
//---------------------------------------------------------------------------

//                        for (int i = 0; i < size ; i++) {
//                            if (!TextUtils.isEmpty(bean.getSku_id())) {
//                                if (bean.getGoods_id().equals(singleAllocationBean.getCate_info().get(max_position).getData().get(i).getGoods_id())
//                                        && bean.getSku_id().equals(singleAllocationBean.getCate_info().get(max_position).getData().get(i).getSku_id())){
//                                    updatePosition = i;
//                                }
//                            }else {
//                                if (bean.getGoods_id().equals(singleAllocationBean.getCate_info().get(max_position).getData().get(i).getGoods_id())){
//                                    updatePosition = i;
//                                }
//                            }
//
//                            if (!TextUtils.isEmpty(cSkuId) && !TextUtils.isEmpty(singleAllocationBean.getCate_info().get(max_position).getData().get(i).getSku_id())) {
//                                if (cGoodId.equals(singleAllocationBean.getCate_info().get(max_position).getData().get(i).getGoods_id())
//                                        && cSkuId.equals(singleAllocationBean.getCate_info().get(max_position).getData().get(i).getSku_id())){
//                                    updatePosition = i;
//                                    bean.setGoods_nums((Double.parseDouble(bean.getGoods_nums()) +
//                                            Double.parseDouble(singleAllocationBean.getCate_info().get(max_position).getData().get(updatePosition).getGoods_nums()))
//                                            + "");
//                                    bean.setGoods_price(jsonObject.optString("price"));
//                                    deletePosition = i;
//                                }
//                            }else {
//                                if (cGoodId.equals(singleAllocationBean.getCate_info().get(max_position).getData().get(i).getGoods_id())){
//                                    updatePosition = i;
//                                    bean.setGoods_nums((Double.parseDouble(bean.getGoods_nums()) +
//                                            Double.parseDouble(singleAllocationBean.getCate_info().get(max_position).getData().get(updatePosition).getGoods_nums()))
//                                            + "");
//                                    bean.setGoods_price(jsonObject.optString("price"));
//                                    deletePosition = i;
//                                }
//                            }
//                        }
//                        if (deletePosition != -1)
//                            singleAllocationBean.getCate_info().get(max_position).getData().remove(deletePosition);
//                        if(updatePosition < deletePosition){
//                            singleAllocationBean.getCate_info().get(max_position).getData().set(updatePosition, bean);
//
//                        } else {
//                            singleAllocationBean.getCate_info().get(max_position).getData().set(updatePosition-1, bean);
//                        }
                        mAdapter.notifyDataSetChanged();
                        my_listView.invalidate();
                    }
                    closeDialog();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {

                super.onFinished();
            }
        });
    }

    private String getPrice() {
        double total = 0;
        for (int i = 0; i < singleAllocationBean.getCate_info().size(); i++) {
            for (int j = 0; j < singleAllocationBean.getCate_info().get(i).getData().size(); j++) {
                double nums = 1;
                try {
                    nums = Double.parseDouble(singleAllocationBean.getCate_info().get(i).getData().get(j).getGoods_nums());
                } catch (Exception e) {
                    nums = 1.0;
                }
                double price = 0;
                try {
                    price = Double.parseDouble(singleAllocationBean.getCate_info().get(i).getData().get(j).getGoods_price());
                } catch (Exception e) {
                    price = 0.0;
                }
                total += nums * price;
            }
        }
        DecimalFormat df = new DecimalFormat("######0.00");
        return df.format(total);
    }


    private AllocationBean a2d(AllocationBean bean) {
        AllocationBean dataEntity = new AllocationBean();
        dataEntity.setIs_old(bean.getIs_old());
        dataEntity.setGoods_id(bean.getGoods_id());
        dataEntity.setGoods_name(bean.getGoods_name());
        dataEntity.setGoods_brand(bean.getGoods_brand());
        dataEntity.setGoods_pic(bean.getGoods_pic());
        dataEntity.setGoods_price(bean.getGoods_price());
        dataEntity.setGoods_nums(bean.getGoods_nums());
        dataEntity.setGoods_unit(bean.getGoods_unit());
        dataEntity.setSku_id(bean.getSku_id());
        dataEntity.setSku_info(bean.getSku_info());
        return dataEntity;
    }

    private double getdoublePrice(double price) {
        BigDecimal d = new BigDecimal(price);
        return d.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Message msg = new Message();
        if (intent.getAction().equals(Constants.UPDATE_PRICE_ADD)) {
//            if (MyUtils.isFastClick()) {
                AllocationBean bean = (AllocationBean) intent.getSerializableExtra("bean");
                msg.what = 100;
                msg.obj = bean;
                msg.arg1 = intent.getIntExtra("position", 0);
                mHandler.sendMessage(msg);
//            }
        } else if (intent.getAction().equals(Constants.UPDATE_FIRST_PRICE)) {
//            if (MyUtils.isFastClick()) {
                LogUtil.e("==输入数量改变价格==", "");
                AllocationBean bean = (AllocationBean) intent.getSerializableExtra("bean");
                msg.what = 102;
                msg.obj = bean;
                msg.arg1 = intent.getIntExtra("position", 0);
                msg.arg2 = intent.getIntExtra("max_position", 0);
                mHandler.sendMessage(msg);
//            }
        } else if (intent.getAction().equals(Constants.UPDATE_PRICE_DEL)) {
//            if (MyUtils.isFastClick()) {
                msg.what = 101;
                msg.arg1 = intent.getIntExtra("position", 0);
                msg.arg2 = intent.getIntExtra("max_position", 0);
                mHandler.sendMessage(msg);
//            }
        }
    }

    private final MyHandler mHandler = new MyHandler(this);

    private class MyHandler extends Handler {
        //对Activity的弱引用
        private final WeakReference<SingleAllocationActivity> mActivity;

        public MyHandler(SingleAllocationActivity activity) {
            mActivity = new WeakReference<SingleAllocationActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            SingleAllocationActivity activity = mActivity.get();
            if (activity == null) {
                super.handleMessage(msg);
                return;
            }
            switch (msg.what) {
                case 100:
                    activity.mAdapter.notifyDataSetChanged();
                    /*String packagepeice = MyApplication.getInstance().getPackagePrice();
                    String goodsprice = MyApplication.getInstance().getPackagePrice_GoodsPrice();
                    Double goodsprice_d = Double.parseDouble(goodsprice);
                    DecimalFormat df = new DecimalFormat("######0.00");
                    double chajia = goodsprice_d * activity.sysnum;
//                    Double sysnums = MyApplication.getInstance().getSingleAllocationBean().getCate_info().get()

                    double all_endPrice = Double.parseDouble(activity.tv_all_price.getText().toString()) + goodsprice_d;
                    LogUtil.e("=增加价格==", all_endPrice + "");

                    activity.tv_all_price.setText("" + df.format(all_endPrice));
                    MyApplication.getInstance().setPackagePrice(activity.tv_all_price.getText().toString());*/


                    packageDiyAdd2(msg.arg1, (AllocationBean) msg.obj);

                    break;
                case 101://单个删除
                    activity.mAdapter.notifyDataSetChanged();
                    /*Double packageprice = Double.parseDouble(MyApplication.getInstance().getPackagePrice());
                    String goodsprice2 = MyApplication.getInstance().getPackagePrice_GoodsPrice();
                    Double goodsprice_d2 = Double.parseDouble(goodsprice2);
                    DecimalFormat df2 = new DecimalFormat("######0.00");
//                    double chajia2 = goodsprice_d2*activity.sysnum;
                    double all_endPrice1 = Double.parseDouble(activity.tv_all_price.getText().toString()) - goodsprice_d2;

                    LogUtil.e("=减少价格==", all_endPrice1 + "");
                    if (packageprice < all_endPrice1) {
                        activity.tv_all_price.setText("" + df2.format(all_endPrice1));
                    } else {
                        activity.tv_all_price.setText("" + df2.format(all_endPrice1));
                    }
                    MyApplication.getInstance().setPackagePrice(activity.tv_all_price.getText().toString());*/
                    packageDiyDel2(msg.arg2, msg.arg1,false);
                    break;
                case 102:
                    activity.mAdapter.notifyDataSetChanged();
                    Double packageprice3 = Double.parseDouble(MyApplication.getInstance().getPackagePrice());
                    String goodsprice3 = MyApplication.getInstance().getPackagePrice_GoodsPrice();
                    if (!TextUtils.isEmpty(goodsprice3)) {
                        Double goodsprice_d3 = Double.parseDouble(goodsprice3);//单价
                        Log.e("商品单价", goodsprice3);
                        DecimalFormat df3 = new DecimalFormat("######0.00");
                        Double oldnums = MyApplication.getInstance().getYsnum();
                        Log.e("商品旧数量", "" + oldnums);
                        Double nownums = MyApplication.getInstance().getInputnums();
                        Log.e("商品新数量", "" + nownums);
                        Double cha_nums = nownums - oldnums;
                        Log.e("商品数量差", "" + cha_nums);
                        Double chajia3 = goodsprice_d3 * cha_nums;
                        Log.e("商品差价", "" + chajia3);
                        double all_endprice3 = Double.parseDouble(activity.tv_all_price.getText().toString()) + chajia3;
//                    activity.tv_all_price.setText("" + df3.format(all_endprice3));
                        //重新设置旧的数量

                        if (nownums < 1){
                            nownums = CalculateUtil.round(nownums,1);
                        }


                       MyApplication.getInstance().setYsnum(nownums);

//                        MyApplication.getInstance().setPackagePrice(activity.tv_all_price.getText().toString());

                        if (MyUtils.isEqual(oldnums,nownums)){
                            return;
                        }
                        if (cha_nums > 0) {
                            packageDiyEditAdd(msg.arg1,(AllocationBean) msg.obj,oldnums,nownums);
                        }else {
                            packageDiyEditDel(msg.arg2, msg.arg1,false,oldnums,nownums);
                        }
                    }else {
                        MyToast("网络不佳,请稍后再试...");
                    }
                    break;
                default:
                    super.handleMessage(msg);
                    break;
            }
        }
    }
}
